<template>
	<div>
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>权限管理</el-breadcrumb-item>
			<el-breadcrumb-item>角色列表</el-breadcrumb-item>
		</el-breadcrumb>
		<el-card>
			<el-button type="primary" @click="addUser">添加角色</el-button>
			<el-table :data="RolesList" border stripe>
				<!-- 展开列 -->
				<el-table-column type="expand">
					<template slot-scope="scope">
						<el-row class="Rowcenter" v-for="(item1,i1) in scope.row.children"
							:class="['expand', i1=== 0 ? 'expandTop' : '']" :key="item1.id">
							<!-- 渲染一级权限 -->
							<el-col :span="5">
								<el-tag @close="removeItem3(scope.row,item1.id)" closable>{{item1.authName}}</el-tag><i
									class="el-icon-caret-right"></i>
							</el-col>
							<!-- 渲染二级权限三级权限 -->
							<el-col :span="19">
								<el-row class="Rowcenter" :class="[i2===0?'':'expandTop']"
									v-for="(item2, i2) in item1.children" :key="item2.id">
									<el-col :span="6">
										<el-tag type="success" @close="removeItem3(scope.row,item2.id)" closable>
											{{item2.authName}}
										</el-tag><i class="el-icon-caret-right"></i>
									</el-col>
									<el-col :span="18">
										<el-tag type="warning" @close="removeItem3(scope.row,item3.id)" closable
											v-for="(item3) in item2.children" :key="item3.id">{{item3.authName}}
										</el-tag>
									</el-col>
								</el-row>
							</el-col>
						</el-row>
					</template>
				</el-table-column>
				<el-table-column type="index"></el-table-column>
				<el-table-column label="角色名称" prop="roleName"></el-table-column>
				<el-table-column label="角色描述" prop="roleDesc"></el-table-column>
				<el-table-column label="操作" width="300px">
					<template slot-scope="scope">
						<el-button size="mini" type="primary" icon="el-icon-edit" @click="compile(scope.row.id)">编辑
						</el-button>
						<el-button size="mini" type="danger" icon="el-icon-delete" @click="removeUser(scope.row.id)">删除
						</el-button>
						<el-button size="mini" type="warning" icon="el-icon-setting"
							@click="showSetRightDialog(scope.row)">分配权限</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<el-dialog title="分配权限" @close="closeDialog" :visible.sync="dialogVisible" width="50%">
			<!-- 树形控件 -->
			<el-tree ref="treeRef" :default-checked-keys="defKeys" show-checkbox node-key="id" default-expand-all
				:data="RolesTree" :props="defaultProps"></el-tree>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="allotRights">确 定</el-button>
			</span>
		</el-dialog>
		<!-- 添加角色 -->
		<el-dialog title="添加角色" @close="closeDialogA" :visible.sync="dialogA" width="50%">
			<el-form :model="addRoles" :rules="compileRules" ref="ruleForm" label-width="100px">
				<el-form-item label="角色名称" prop="roleName">
					<el-input v-model="addRoles.roleName"></el-input>
				</el-form-item>
				<el-form-item label="角色描述" prop="roleDesc">
					<el-input v-model="addRoles.roleDesc"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogA = false">取 消</el-button>
				<el-button type="primary" @click="dialogUser">确 定</el-button>
			</span>
		</el-dialog>
		<!-- 编辑 -->
		<el-dialog title="修改角色" @close="compileDialog" :visible.sync="compileFormVisible">
			<el-form :model="compileform" :rules="compileRules" ref="compileRule">
				<el-form-item label="角色名称" label-width="100px" prop="roleName">
					<el-input v-model="compileform.roleName"></el-input>
				</el-form-item>
				<el-form-item label="角色描述" label-width="100px" prop="roleDesc">
					<el-input v-model="compileform.roleDesc"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer">
				<el-button @click="compileFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="uploading">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				compileFormVisible: false,
				compileRules: {
					roleName: [{
							required: true,
							message: '请输入角色名称',
							trigger: 'blur'
						}
					],
					roleDesc: [{
						required: true,
						message: '请输入角色描述',
						trigger: 'blur',
					}],
				},
				RolesList: [],
				RolesTree: [],
				addRoles: {
					roleName: '',
					roleDesc: ''
				},
				compileform: {
					roleName: '',
					roleDesc: ''
				},
				dialogVisible: false,
				dialogA: false,
				defaultProps: {
					label: 'authName',
					children: 'children'
				},
				defKeys: [],
				UserId: 0,
			}
		},
		created() {
			this.getRolesList();
		},
		methods: {
			compileDialog(){
				this.$refs.compileRule.resetFields()
			},
			 uploading() {
				this.$refs.compileRule.validate(async msg=>{
					if(!msg)return this.$message.error('请输入正确！')
					const {data:res} = await this.$http.put(`roles/${this.UserId}`,this.compileform)
					if(res.meta.status!==200)return this.$message.error('更新数据失败！')
					this.$message.success('更新成功！')
					this.getRolesList()
					this.compileFormVisible=false
				})
			},
			async compile(ID) {
				this.UserId = ID
				const {
					data: res
				} = await this.$http.get(`roles/${ID}`)
				if (res.meta.status !== 200) return this.$message.error('获取角色数据失败！')
				this.compileform.roleName = res.data.roleName
				this.compileform.roleDesc = res.data.roleDesc
				this.compileFormVisible = true
			},
			async removeUser(ID) {
				const {
					data: msg
				} = await this.$http.delete(`roles/${ID}`)
				if (msg.meta.status !== 200) return this.$message.error('删除失败！')
				this.$message.success('删除成功')
				this.getRolesList();
			},
			closeDialogA() {
				this.$refs.ruleForm.resetFields()
			},
			dialogUser() {
				this.$refs.ruleForm.validate(async msg => {
					if (!msg) return this.$message.error('请输入正确！')
					const {
						data: res
					} = await this.$http.post('roles', this.addRoles)
					if (res.meta.status !== 201) return this.$message.error('添加角色失败！')
					this.getRolesList()
					this.dialogA = false
				})
			},
			addUser() {
				this.dialogA = true
			},
			async allotRights() {
				const keys = [
					...this.$refs.treeRef.getCheckedKeys(),
					...this.$refs.treeRef.getHalfCheckedKeys()
				]
				const idStr = keys.join(',');
				var {
					data: res
				} = await this.$http.post(`roles/${this.UserId}/rights`, {
					rids: idStr
				})
				if (res.meta.status !== 200) return this.$message.error('分配权限失败！');
				this.$message.success('分配权限成功')
				this.getRolesList()
				this.dialogVisible = false
			},
			closeDialog() {
				this.defKeys = []
			},
			getLeafKeys(node, arr) {
				if (!node.children) {
					return arr.push(node.id)
				}
				node.children.forEach(item => this.getLeafKeys(item, arr))
			},
			async showSetRightDialog(row) {
				this.UserId = row.id
				const {
					data: res
				} = await this.$http.get('rights/tree')
				if (res.meta.status !== 200) return this.$message.error('获取权限列表失败！')
				this.RolesTree = res.data
				this.getLeafKeys(row, this.defKeys);
				this.dialogVisible = true;
			},
			removeItem3(roleId, rightId) {
				this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(async () => {
					const {
						data: res
					} = await this.$http.delete(`roles/${roleId.id}/rights/${rightId}`)
					if (res.meta.status !== 200) return this.$message.error('删除失败！')
					roleId.children = res.data
					this.$message({
						type: 'success',
						message: '删除成功!'
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			async getRolesList() {
				var {
					data: res
				} = await this.$http.get('roles')
				if (res.meta.status !== 200) return this.$message.error('获取角色列表失败')
				this.RolesList = res.data
			}
		},
	}
</script>

<style scoped>
	.Rowcenter {
		display: flex;
		align-items: center;
	}

	.el-table .cell {
		width: 100% !important;
	}

	.el-tag {
		margin: 7px;
	}

	.expand {
		border-bottom: 1px solid #eee;
	}

	.expandTop {
		border-top: 1px solid #eee;
	}
</style>
